<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProductController
 *
 * @author <KTD>
 */
class ProductController extends BackController
{

    public $modelName = 'Product';

    public function init()
    {
        parent::init();
        $this->typeMyActiveForm = TRUE;
        $this->controllerMenu   = array();
        $this->controllerMenu[] = array(
            'label' => Yii::t('post', 'All'),
            'url'   => $this->createUrl('product/index', array('lang' => $this->lang))
        );
    }

    public function save($action = 'Create')
    {

        $status = parent::save($action);
        if ($status)
        {
            $this->model->address     = $_POST['Product']['address'];
            $this->model->description = $_POST['Product']['description'];
            $this->model->content     = $_POST['Product']['content'];
            $this->model->save();
            /*
             * Khi tao moi bai viet thi luu luon ID cua bai post do cho revision_id
             */
            if ($action == 'Create')
            {
                $this->model->created_at = time();
                $this->model->save();
            }
            /*
             * luu anh vao bang product_image
             */

//            if (isset($_POST['ProductMeta']))
//            {
//                foreach ($_POST['ProductMeta'] as $id => $info)
//                {
//                    $item = ProductImage::model()->findByPk($id, 'pid=:p', array(
//                        ':p' => $this->model->id
//                    ));
//
//                    if ($item == null)
//                    {
//                        $item = new ProductImage();
//                        $item->pid = $this->model->id;
//                    }
//                    $item->image = $info['file'];
//                    $status = $item->save();
//                }
//            }
            /*
             * xoa anh duoc chon trong bang product_image
             */
//            if (isset($_POST['deleteItem']))
//            {
//                foreach ($_POST['deleteItem'] as $id)
//                {
//                    ProductImage::model()->deleteByPk($id, 'pid  = :p ', array(
//                        ':p' => $this->model->id
//                    ));
//                }
//            }
        }

        return $status;
    }

}

?>
